//
//  HRGiftView.m
//  SpeedDating
//
//  Created by ISU1 on 17/2/7.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "HRGiftView.h"
#import "HRMessageGiftCollectionViewCell.h"
#import "XLLayout.h"
#import "UIButton+Category.h"
@interface HRGiftView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) UIButton *charmLabel;
@property (nonatomic, strong) UIButton *charmImage;

@property (nonatomic, strong) UIButton *rechargeButton;

@property (nonatomic, strong) UIButton *sendButton;

@property (nonatomic, strong) NSMutableArray *giftArray;

@property (nonatomic, assign) NSInteger index;



@end

@implementation HRGiftView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
    
        XLLayout *layout = [[XLLayout alloc] init];
        layout.itemSize = CGSizeMake(63, 63);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:layout];
        [self.collectionView registerClass:[HRMessageGiftCollectionViewCell class] forCellWithReuseIdentifier:@"ReuseIdentifier"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor blackColor];
        [self addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self);
            make.bottom.equalTo(self).offset(-40);
        }];
        
        _charmLabel = [[UIButton alloc] init];
        [_charmLabel setTitle:@" 充值 " forState:UIControlStateNormal];
        [_charmLabel setTitleColor:ColorI(0xfdd815) forState:UIControlStateNormal];
        [_charmLabel setImage:[UIImage imageNamed:@"viedo_recharge_back_icon"] forState:UIControlStateNormal];
        [_charmLabel tranpositionImageAnTitleLable];
        [self addSubview:_charmLabel];
        
        [_charmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.height.mas_equalTo(20);
            make.bottom.equalTo(self).offset(-10);
        }];
        
        _charmImage = [[UIButton alloc] init];
        [_charmImage setTitle:@" 0      " forState:UIControlStateNormal];
        [_charmImage setTitleColor:ColorI(0xfdd815) forState:UIControlStateNormal];
        [_charmImage setImage:[UIImage imageNamed:@"grzx_account_diamond_icon"] forState:UIControlStateNormal];
        [_charmImage tranpositionImageAnTitleLable];
        [self addSubview:_charmImage];

        [_charmImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_charmLabel.mas_right).offset(10);
            make.height.mas_equalTo(20);
            make.bottom.equalTo(self).offset(-10);
        }];
        
        
        _rechargeButton = [[UIButton alloc] init];
        [self addSubview:_rechargeButton];
        [_rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.equalTo(_charmLabel);
        }];
        [_rechargeButton addTarget:self action:@selector(clickRechargeButton) forControlEvents:UIControlEventTouchUpInside];
        
        
        _sendButton = [[UIButton alloc] init];
        [_sendButton setBackgroundImage:[UIImage imageNamed:@"viedo_gift_give_button"] forState:UIControlStateNormal];
        [_sendButton setTitle:@"赠送" forState:UIControlStateNormal];
        _sendButton.backgroundColor = [UIColor blackColor];
        _sendButton.titleLabel.font = FontP(15);
        
        [self addSubview:_sendButton];
        [_sendButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-15);
            make.bottom.equalTo(self).offset(-8);
            make.height.mas_equalTo(30);
            make.width.mas_equalTo(60);
        }];
        [_sendButton addTarget:self action:@selector(clickSendButton) forControlEvents:UIControlEventTouchUpInside];
        [self sendRequest];
    }
    return self;
}

- (void)setIsShow:(BOOL)isShow {
    self.hidden = isShow;
    if (!isShow) {
        [self sendRequest];
    }
}

- (void)sendRequest {
    WEAKSELF
    [HQHttpManager userInfo2WithUid:[HQHttpManager getUserId] successBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        if (isSuccess) {
            
            
            [weakSelf.charmImage setTitle:[NSString stringWithFormat:@" %@      ",resultDic[@"charm"]] forState:UIControlStateNormal];
            weakSelf.charmLabel.titleLabel.font = FontP(15);
            weakSelf.charmImage.titleLabel.font = FontP(12);

            [weakSelf.charmLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakSelf).offset(15);
                make.height.mas_equalTo(20);
                make.bottom.equalTo(weakSelf).offset(-10);
            }];
            [weakSelf.charmImage mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(weakSelf.charmLabel.mas_right).offset(10);
                make.height.mas_equalTo(20);
                make.bottom.equalTo(self).offset(-10);
            }];
            
            
            [weakSelf.rechargeButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.top.bottom.equalTo(weakSelf.charmLabel);
            }];
        }
    }];
    [HQHttpManager giftListWithSuccessBlock:^(BOOL isSuccess, NSDictionary *resultDic) {
        if (isSuccess) {
            
            weakSelf.giftArray = [CheekGiftModel mj_objectArrayWithKeyValuesArray:resultDic[@"res"]];
            
            [weakSelf.collectionView reloadData];
        }
    }];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _giftArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString * CellIdentifier = @"ReuseIdentifier";
    
    
    
    HRMessageGiftCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor blackColor];
    cell.model =_giftArray[indexPath.item];
    
    if ([_timeType isEqualToString:@"0"]) {
        cell.timeLab.hidden = YES;
    }else{
        cell.timeLab.hidden = NO;
    }
    if (indexPath.item == _index) {
        cell.isSelect = YES;
    } else {
        cell.isSelect = NO;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((KScreenWidth-5)/3.0, (KScreenWidth-5)/3.0/9.0*11.0);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    _index = indexPath.item;
    [self.collectionView reloadData];
}

- (void)clickRechargeButton {
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(didClickRechargeButton)]) {
        [weakSelf.delegate didClickRechargeButton];
    }
}

- (void)clickSendButton {
    CheekGiftModel *model = _giftArray[_index];
    WEAKSELF
    if ([weakSelf.delegate respondsToSelector:@selector(didClickSendButton:)]) {
        [weakSelf.delegate didClickSendButton:model];
    }
}

@end
